<?php

/**
 * celebrityVote actions.
 *
 * @package    iEasy
 * @subpackage celebrityVote
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class celebrityVoteActions extends sfActions
{
    /**
     * Executes index action
     *
     * @param sfRequest $request A request object
     */
    public function executeIndex(sfWebRequest $request)
    {
        $this->forward('default', 'module');
    }

    public function executeList(sfWebRequest $request)
    {
        $this->theme = $request->getParameter('theme');
        $this->component = $request->getParameter('component');
        //        if ($request->isXmlHttpRequest())
        $this->renderComponent('celebrityVote', $this->component);
    }


    public function executeVote(sfWebRequest $request)
    {
        try {
            $ip = utility::getRequestIp($request);
            $userId = $this->getUser()->getAttribute('userId');
            if ($userId == '' || $userId == null) $userId = 1;
            $voteId = $request->getParameter("voteId");
            $log = Doctrine_Core::getTable('Celebrityvotelog')->createQuery('c')
                    ->addWhere('c.user_ip=?', $ip)->addWhere('c.vote_id=?', $voteId)
                    ->addOrderBy('c.create_time desc')->fetchOne();
            if ($log != null) {
                $now = time();
                $latestLogTime = strtotime($log->getCreateTime());
                $diff = $now - $latestLogTime;
                if (($diff / 60) < 5)
                    return $this->renderText('0');
            }


            $vote = Doctrine_Core::getTable('celebrityvote')->find($voteId);
            $vote->setVoteI($vote->getVoteI() + 1);
            $vote->save();

            $log = new Celebrityvotelog();
            $log->setUserId($userId);
            $log->setCreateTime(date(Constants::$DEFAULT_DATETIME_FORMAT));
            $log->setUserIp($ip);
            $log->setVoteId($voteId);
            $log->save();

            if ($request->isXmlHttpRequest()) {
                $theme = $request->getParameter('theme');
                return $this->renderComponent('celebrityVote', 'candidateList', array('theme' => $theme));
            }
        } catch (Exception $e) {
        }
    }
}
